<template>
  <div class="opration">
    <div class="oprationLeft">
      <el-form :inline="true" size="small" :model="searchForm">
        <el-form-item :label="$t('操作时间')">
          <!-- <el-date-picker
            v-model="searchForm.startTime"
            type="datetime"
            placeholder="开始日期"
            format="yyyy 年 MM 月 dd 日 HH:mm:ss"
            default-time="23:59:59"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker> -->
          <el-date-picker
            v-model="searchForm.startTime"
            type="date"
            :placeholder="$t('开始日期')"
            format="yyyy-MM-dd"
            default-time="23:59:59"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="至">
          <el-date-picker
            v-model="searchForm.endTime"
            type="date"
            :placeholder="$t('结束日期')"
            format="yyyy-MM-dd"
            default-time="23:59:59"
            value-format="yyyy-MM-dd"
            :picker-options="{
              disabledDate(time) {
                if (!searchForm.startTime) return false
                return (
                  time.getTime() <
                  new Date(searchForm.startTime).getTime() - 3600000 * 24
                )
              },
            }"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmmit">{{
            $t('筛选')
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="oprationRight">
        <el-form :inline="true" size="small" :model="searchForm">
            <el-form-item>
                <el-input :placeholder="$t('请输入关键词搜索')" v-model="searchForm.queryParam" clearable style="width:160px;">
                <template slot="prepend"><i class="iconfont icon-sousuo" /></template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <bs-search-button-icon @click.native="onSubmmit"></bs-search-button-icon>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'OperationLogCondition',
  data() {
    return {
      searchForm: {
        startTime: '',
        endTime: '',
      },
      node: {},
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initDate()
    })
  },
  components: {},
  methods: {
    initDate() {
      this.searchForm.startTime = getNowFormatDate()
      this.searchForm.endTime = getNowFormatDate()
      this.onSubmmit()
    },
    currentNode(node) {
      this.node = node
      this.queryLog()
    },
    onSubmmit() {
      // if (!this.searchForm.startTime || this.searchForm.startTime === null) {
      //   this.$message.error(this.$t('请选择开始时间'))
      //   return
      // }
      const startTime = new Date(this.searchForm.startTime).getTime()
      const endTime = new Date(this.searchForm.endTime).getTime()
      if (startTime > endTime) {
        this.$message.error(this.$t('开始时间不能大于结束时间'))
        return
      }
      
      let params = {}
      if (this.searchForm.startTime) {
        params.startTime = this.searchForm.startTime + ' 00:00:00'
      }
      if (this.searchForm.endTime) {
        params.endTime = this.searchForm.endTime + ' 23:59:59'
      }
      if (this.searchForm.queryParam) {
        params.queryParam = this.searchForm.queryParam
      }
      if (this.node.username != '所有用户') {
        params.username = this.node.username
      }
      console.log('onSubmmit ---------- ' ,params)
      this.$emit('filter-loglist', params)
    },
    queryLog() {
      // if (!this.searchForm.startTime || this.searchForm.startTime === null) {
      //   this.searchForm.startTime = getNowFormatDate()
      // }
      // if (!this.searchForm.endTime || this.searchForm.endTime === null) {
      //   this.searchForm.endTime = getNowFormatDate()
      // }
      const startTime = new Date(this.searchForm.startTime).getTime()
      const endTime = new Date(this.searchForm.endTime).getTime()
      if (startTime > endTime) {
        this.$message.error(this.$t('开始时间不能大于结束时间'))
        return
      }
      console.log('select time = ' + this.searchForm.endTime)
      let params = {}
      params.startTime = this.searchForm.startTime + ' 00:00:00'
      params.endTime = this.searchForm.endTime + ' 23:59:59'
      if (this.node.username != '所有用户') {
        params.username = this.node.username
      }
      this.$emit('filter-loglist', params)
    },
  },
}
function getNowFormatDate() {
  var date = new Date()
  var seperator1 = '-'
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate
}
</script>
<style lang="scss" scoped>
// import '@/styles/bs-common.scss';
.oprationLeft {
  ::v-deep .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    width: 180px;
  }
}
</style>
